package com.leyou.user.controller;

import com.leyou.user.entity.DeliveryAddress;
import com.leyou.user.service.DeliveryAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeliveryAddressController {
    @Autowired
    private DeliveryAddressService deliveryAddressService;
    /**
     * 查询用户所有收货地址
     * @return
     */
    @GetMapping("/address/all")
    public ResponseEntity<List<DeliveryAddress>> queryUserAllDeliveryAddress(){
        List<DeliveryAddress>  deliveryAddressList = deliveryAddressService.queryUserAllDeliveryAddress();
        return ResponseEntity.ok(deliveryAddressList);
    }
    /**
     * 新增收货地址
     */
    @PostMapping("/address/add")
    public ResponseEntity<Void> addUserDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress){
        deliveryAddressService.addUserDeliveryAddress(deliveryAddress);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    /**
     * 根据id查询收货地址
     */
    @GetMapping("/address/one")
    public ResponseEntity<DeliveryAddress> queryDeliveryAddressById(@RequestParam("id") Long id){
        DeliveryAddress deliveryAddress =  deliveryAddressService.queryDeliveryAddressById(id);
        return ResponseEntity.ok(deliveryAddress);
    }
    /**
     * 删除收货地址
     */
    @DeleteMapping("/address/delete")
    public  ResponseEntity<Void> deleteDeliveryAddressById(@RequestParam("id") Long id){
        deliveryAddressService.deleteDeliveryAddressById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    /**
     * 更新收货地址
     */
    @PutMapping("/address/update")
    public ResponseEntity<Void> updateDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress){
        deliveryAddressService.updateDeliveryAddress(deliveryAddress);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    /**
     * 查询是否可是可设置默认地址
     */
    @GetMapping("/address/isDefault")
    public ResponseEntity<Boolean> queryHasIsDefault(){
       Boolean result =  deliveryAddressService.queryHasIsDefault();
       return ResponseEntity.ok(result);
    }
    /**
     * 修改默认地址
     */
    @PutMapping("/address/update/isDefault")
    public ResponseEntity<Void> updateDefaultDeliveryAddress(@RequestBody DeliveryAddress deliveryAddress){
        deliveryAddressService.updateDefaultDeliveryAddress(deliveryAddress);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
